<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\BatchRestore;
use App\Admin\Actions\Grid\Restore;
use App\Admin\Repositories\User;
use App\Models\Role;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class UserController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new User(), function (Grid $grid) {

            $grid->model()->with('roles');

            $grid->showColumnSelector();
            $grid->disableCreateButton();
            $grid->disableViewButton();
            $grid->quickSearch('name', 'email');

            $grid->column('id')
                ->sortable()
                ->hide();

            $grid->column('avatar')
                ->image('', 50, 50);

            $grid->column('name');

            $grid->column('email');

            $grid->column('email_verified_at')
                ->sortable()
                ->hide();

            $grid->column('roles')->display(function ($roles) {
                return implode(',', $roles->pluck('name')->toArray());
            });

            $grid->column('notification_count')
                ->hide();

            $grid->column('last_actived_at')
                ->sortable()
                ->display(function ($value) {
                    return $value->diffForHumans();
                });

            $grid->column('created_at')
                ->sortable()
                ->hide();

            $grid->column('updated_at')
                ->sortable()
                ->hide();

            // 回收站
            $grid->filter(function (Grid\Filter $filter) {
                // 范围过滤器，调用模型的`onlyTrashed`方法，查询出被软删除的数据。
                $filter->scope('trashed', '回收站')->onlyTrashed();
                $filter->like('name');
                $filter->like('email');
            });
            // 行恢复
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                if (request('_scope_') == 'trashed') {
                    $actions->append(new Restore(\App\Models\User::class));
                }
            });
            // 批量恢复
            $grid->batchActions(function (Grid\Tools\BatchActions $batch) {
                if (request('_scope_') == 'trashed') {
                    $batch->add(new BatchRestore(\App\Models\User::class));
                }
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {

        return Form::make(User::with(['roles']), function (Form $form) {

            $id = $form->getKey();
            $form->hidden('id');

            $form->text('name')
                ->required()
                ->rules('required|between:3,25|regex:/^[A-Za-z0-9\-\_]+$/|unique:users,name,' . $id);

            $form->email('email')
                ->required()
                ->rules('required|email|unique:users,email,' . $id);

            $form->multipleSelect('roles')
                ->options(Role::all()->pluck('name', 'id'))
                ->customFormat(function ($roles) {

                    if (! $roles) {
                        return [];
                    }

                    // 从数据库中查出的二维数组中转化成ID
                    return array_column($roles, 'id');
                });

            $form->image('avatar')
                ->uniqueName()
                ->accept('jpg,png,gif,jpeg')
                ->rules('mimes:png,jpg,gif,jpeg|dimensions:min_width=208,min_height=208');

            $form->textarea('introduction')
                ->rules('max:80');

        });
    }
}
